<template>
	<view>
		<u-popup :show="updateShow" mode="center" round="16">
			<view class="update-popup">
				<image src="https://hltxliang.67910000.com/static/update-modal-bg.png" mode=""></image>
				<view class="title">
					发现新版本
					<text>v{{ onlineVersion }}</text>
				</view>
				<view class="wrap">
					<view class="text">
						<view class="subtitle">更新内容:</view>
						<view v-for="(item, i) in updateContent" :key="i">{{ i + 1 }}.{{ item }}</view>
					</view>
					<view class="footer">
						<u-button @click="appUpdate" class="btn" shape="square">立即更新</u-button>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name: "update-popup",
		data() {
			return {
				onlineVersion: '', //当前版本号
				updateShow: false, //弹窗
				updateContent: [
					//更新文本
					'修复其他已知bug'
				]
			};
		},
		mounted() {
			// #ifdef APP-PLUS
			this.AppCheckUpdate();
			// #endif
		},
		methods: {
			// 检查app更新
			async AppCheckUpdate() {
				let platform = uni.getSystemInfoSync().platform;
				let version = this.$version;
				//这个接口让后端返回新版本的信息
				let res = await this.$api.getConfig({
					name:'version'
				})
				// console.log(plus.runtime.uniVersion)
				// 传入用户当前版本号
				let onlineAndroidVersion = res.data;
				let onlineVersion;
				this.onlineVersion = onlineAndroidVersion;
				onlineVersion = onlineAndroidVersion.split('.').join('');
				console.log(onlineAndroidVersion)
				version = version.split('.').join('');
				console.log('onlineVersion:' + onlineVersion);
				console.log('version:' + version);
				let [bigVersion, smallVersion] = [version.slice(0, 2), version.slice(2)];
				console.log('bigVersion ', bigVersion);
				console.log('smallVersion ', smallVersion);
				// 当前线上大小版本号
				let [onlineBigVersion, onlineSmallVersion] = [onlineVersion.slice(0, 2), onlineVersion.slice(2)];
				console.log('onlineBigVersion ', onlineBigVersion);
				console.log('onlineSmallVersion ', onlineSmallVersion);
				console.log(Number(version) < Number(onlineVersion))
				if (Number(version) < Number(onlineVersion)) {
					if (Number(bigVersion) < Number(onlineBigVersion)) {
						//整包更新
						console.log('整包更新')
						this.updateShow = true;
						// 隐藏底部tab防止用户切换
						// if (!this.maskClose) {
						// 	uni.hideTabBar();
						// }
					} else {
						console.log('热更新')
						// 热更新
						console.log(smallVersion<onlineSmallVersion)
						console.log(onlineSmallVersion)
						console.log(smallVersion)
						if (Number(smallVersion) < Number(onlineSmallVersion)) {
							this.wgtUpdate();
						}
					}
				}
			},
			// 更新wgt包
			wgtUpdate() {
				let platform = uni.getSystemInfoSync().platform;
				uni.showLoading({
					title: '下载更新中，请稍后'
				});
				console.log('下载更新中，请稍后')
				let url = 'https://www.longzhiyue.phpcaff.cn/longzhiyue.wgt';
				var dtask = plus.downloader.createDownload(url, {}, function(d, status) {
					// 下载完成
					console.log(d);
					console.log(status);
					if (status == 200) {
						console.log('下载..');
						uni.hideLoading();
						plus.runtime.install(
							d.filename, {},
							function(res) {
								plus.runtime.restart();
							},
							function(error) {
								console.log('下载失败');
								console.log(error);
								// uni.showToast({
								// 	title: '安装失败',
								// 	mask: false,
								// 	duration: 1500
								// });
							}
						);
					} else {
						uni.hideLoading();
						uni.showToast({
							title: '更新失败',
							mask: false,
							duration: 1500
						});
					}
				});
				dtask.start(); //执行下载
			},
			// 更新整包
			appUpdate() {
				let platform = uni.getSystemInfoSync().platform;
				if (platform == 'android') {
					//downloadUrl接口返回的应用下载地址
					uni.showLoading({
						title: '下载更新中，请稍候'
					});
					let url = 'https://www.longzhiyue.phpcaff.cn/longzhiyue.apk';
					var dtask = plus.downloader.createDownload(url, {}, function(d, status) {
						// 下载完成
						if (status == 200) {
							uni.hideLoading();
							plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(
								error) {
								uni.showToast({
									title: '安装失败',
									mask: false,
									duration: 1500
								});
							});
						} else {
							uni.hideLoading();
							uni.showToast({
								title: '更新失败',
								mask: false,
								duration: 1500
							});
						}
					});
					dtask.start(); //执行下载
				} else if (platform == 'ios') {
					// 跳转苹果商城
					let appleId = '1566982438';
					plus.runtime.launchApplication({
							action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
						},
						function(e) {
							console.log('Open system default browser failed: ' + e.message);
						}
					);
				}
			},
		}
	}
</script>

<style lang="scss">
	.update-popup {
		border-radius: 16px;
		overflow: hidden;

		image {
			width: 560upx;
			height: 310upx;
		}

		.title {
			text-align: center;
			font-size: 60upx;
			font-weight: 600;
			color: #fff;
			background-image: -webkit-linear-gradient(bottom, #0195ff, #92c7ff);
			-webkit-background-clip: text;
			-webkit-text-fill-color: transparent;

			text {
				font-size: 30upx;
			}
		}

		.wrap {
			padding: 30upx;

			.text {
				line-height: 1.6;
				color: #333;

				.subtitle {
					font-size: 32upx;
					margin-bottom: 10upx;
				}
			}

			.footer {
				text-align: center;
				margin-top: 30upx;

				view {
					margin-top: 20upx;
					color: #999;
				}
			}
		}
	}
</style>
